package com.dhh.toodledo.manager;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.json.JSONObject;
import com.dhh.toodledo.model.Task;
import com.dhh.toodledo.util.HttpUtil;
import com.dhh.toodledo.util.JsonUtil;

import android.util.Pair;

public class TaskManager {
	public TaskManager(){}
	
	public Pair<Boolean, List<Task>> getTasks(String key,String start) throws Exception{
		List<Task> tasks = HttpUtil.getTasks(key, start);
		return new Pair<Boolean, List<Task>>(tasks.size()>0,tasks);
	}
	
	
	public Task getTask(String key,String taskId) throws Exception{
		return HttpUtil.getTask(key, taskId);
	}
	
	public Task markDone(String key,Long taskId,boolean isDone){
		Task task = null;
		JSONObject obj = new JSONObject();
		try {
			obj.put("id", taskId.toString());
			obj.put("completed", isDone ? getStamp():"0");
			String json = HttpUtil.updateTask(key, obj);
			if(!JsonUtil.isError(json)){
				task = JsonUtil.parseTask(json);
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		return task;
	}
	
	public Task markStar(String key,Long taskId,Long star){
		Task task = null;
		JSONObject obj = new JSONObject();
		try {
			obj.put("id", taskId.toString());
			obj.put("star", star);
			String json = HttpUtil.updateTask(key, obj);
			if(!JsonUtil.isError(json)){
				task = JsonUtil.parseTask(json);
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return task;
	}
	
	public Task updateTask(String key,Long taskId,Long startdate,Long duedate,Long priority,String note,String title){
		Task task = null;
		JSONObject obj = new JSONObject();
		try {
			obj.put("id", taskId.toString());
			if(startdate!=null)
				obj.put("startdate", startdate);
			if(duedate!=null)
				obj.put("duedate", duedate);
			if(priority!=null)
				obj.put("priority", priority);
			if(note!=null)
				obj.put("note", note);
			if(title!=null)
				obj.put("title", title);
			String json = HttpUtil.updateTask(key, obj);
			if(!JsonUtil.isError(json)){
				task = JsonUtil.parseTask(json);
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return task;
	}
	
	public Task deleteTask(String key,Long taskId){
		Task task = null;
		JSONObject obj = new JSONObject();
		try {
			obj.put("id", taskId.toString());
			String json = HttpUtil.deleteTask(key, obj);
			if(!JsonUtil.isError(json)){
				task = JsonUtil.parseTask(json);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return task;
	}
	
	public Task addTask(String key,Task task){
		JSONObject obj = new JSONObject();
		try {
			obj.put("title", task.getTitle());
			obj.put("star", task.getStar().toString());
			if(task.getStartdate()!=null&&task.getStartdate()>0)
				obj.put("startdate", task.getStartdate().toString());
			if(task.getDuedate()!=null&&task.getDuedate()>0)
				obj.put("duedate", task.getDuedate().toString());
			obj.put("priority", task.getPriority().toString());
			if(task.getNote()!=null)
				obj.put("note", task.getNote().toString());
			String json = HttpUtil.addTask(key, obj);
			if(!JsonUtil.isError(json)){
				task = JsonUtil.parseTask(json);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return task;
	}
	
	private String getStamp() throws ParseException{
		Timestamp appointTime=Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date date = df.parse(String.valueOf(appointTime));
		long s=date.getTime();  
		String unixDate=String.valueOf(s).substring(0, 10);
		return unixDate;
	}
	
	public Task initTask(){
		Task task = new Task();
		task.setPriority(0l);
		return task;
	}
}
